<template>
  <div class="container">
    <el-card class="card">
      <el-form>
        <el-row :gutter="24">
          <el-col :span="6">
            <el-form-item label="关键字" style="margin-left: 35px">
              <el-input
                v-model="page.keyword"
                placeholder="根据编号搜索"
                style="width: 300px"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :offset="15" :span="3">
            <el-form-item>
              <el-button @click="page.keyword = ''">清除</el-button>
              <el-button type="primary" @click="searchBtn">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-alert
        :title="`数据一共${alls}条`"
        show-icon
        :closable="false"
      ></el-alert>

      <el-table :data="list" style="width: 100%">
        <el-table-column prop="id" label="编号" width="220"> </el-table-column>
        <el-table-column prop="questionType" label="题型" width="85">
          <template slot-scope="{ row }">
            <span v-if="row.questionType == 1">单选</span>
            <span v-if="row.questionType == 2">多选</span>
            <span v-if="row.questionType == 3">简答</span>
          </template>
        </el-table-column>
        <el-table-column prop="questionIDs" label="题目编号" width="220">
          <template v-slot="{ row }">
            <el-button
              type="text"
              v-for="(item, index) in row.questionIDs"
              :key="index"
              @click="watchNumber(item.id)"
            >
              {{ item.number }}</el-button
            >
          </template>
          <!-- <el-button type="text" v-for="item in questionIDs" :key="item.id"
            >{{ item.number }}
          </el-button> -->
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间" width="180">
        </el-table-column>
        <el-table-column prop="totalSeconds" label="答题时间(s)" width="295">
        </el-table-column>
        <el-table-column prop="accuracyRate" label="正确率(%)" width="295">
        </el-table-column>
        <el-table-column prop="userName" label="录入人" width="285">
        </el-table-column>
        <el-table-column label="操作" width="60">
          <template slot-scope="{ row }">
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="delBtn(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-row :gutter="20">
        <el-col :offset="14" :span="6">
          <el-pagination
            background
            layout="prev, pager, next,sizes,jumper"
            :total="alls"
            :page-size="page.pagesize"
            :page-sizes="[20, 30, 50, 100]"
            :current-page.sync="page.page"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          >
          </el-pagination>
        </el-col>
      </el-row>
      <!-- 分页 -->
    </el-card>
    <questions-randoms
      :closeDialog="closeDialog"
      :data="watchData"
      @onClose="onClose"
    ></questions-randoms>
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions.js'
import questionsRandoms from '@/module-hmmm/components/questions-preview.vue'
export default {
  name: 'oil',
  components: { questionsRandoms },
  created () {
    this.getList()
  },
  data () {
    return {
      watchData: {}, // 弹层数据
      closeDialog: false, // 弹层显示隐藏
      page: {
        pagesize: 20, // 每页条数
        page: 1, // 当前页码
        keyword: ''
      },
      // input: '', // 输入框
      list: [], // 列表数据
      alls: 0 // 总条数
    }
  },
  methods: {
    async getList () {
      const res = await randoms(this.page)
      this.alls = res.data.counts // 总条数
      this.list = res.data.items
    },
    async delBtn (id) {
      try {
        await this.$confirm(
          '此操作将永久删除该题组, 是否继续?, 是否继续?',
          '提示',
          {
            type: 'warning'
          }
        )
        await removeRandoms(id)
        this.getList()
        this.$message.success('题组删除成功')
      } catch (err) {
        console.log(err)
      }
    },
    async searchBtn () {
      // 搜索
      this.getList()
      // console.log(11111)
      // this.list = res.data.items
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.page.pagesize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.page.page = val
      this.getList()
      // console.log(`当前页: ${val}`)
    },
    async watchNumber (num) {
      // 点击编号显示弹层
      this.closeDialog = true
      const data = await detail(num)
      console.log(data)
      this.watchData = data
    },
    onClose () {
      // 关闭弹层
      this.closeDialog = false
    }
  }
}
</script>

<style scoped lang="less">
.card {
  margin: 17px;
}
</style>
